package com.zjeav.lingjiao.ui.personCenter;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dalimao.corelibrary.pullrefresh.BGANormalRefreshViewHolder;
import com.dalimao.corelibrary.pullrefresh.BGARefreshLayout;
import com.dalimao.corelibrary.pullrefresh.PowerfulRecyclerView;
import com.dalimao.corelibrary.swipeback.SwipeBackActivity;
import com.zjeav.lingjiao.R;
import com.zjeav.lingjiao.base.adapter.DownloadBookAdapter;
import com.zjeav.lingjiao.base.baseBean.BaseApplication;
import com.zjeav.lingjiao.base.baseBean.LocalMetadata;
import com.zjeav.lingjiao.base.tools.FileUtils;
import com.zjeav.lingjiao.base.tools.LocalUtils;
import com.zjeav.lingjiao.base.utils.UIUtils;
import com.zjeav.lingjiao.ui.music.MusicActivity;

import org.xutils.DbManager;
import org.xutils.ex.DbException;

import java.util.ArrayList;
import java.util.List;

public class MyDownLoadActivity extends SwipeBackActivity {

    private BGARefreshLayout mRefreshLayout;
    private PowerfulRecyclerView mBooks;
    private List<LocalMetadata> mp3Infos;
    private DownloadBookAdapter downloadBookAdapter;
    private DbManager db;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_down_load_layout);

        mRefreshLayout = findViewById(R.id.refresh_layout);
        mBooks = findViewById(R.id.rv_books);
        mp3Infos = new ArrayList<>();

        db = BaseApplication.dbManager;

        try {
            downloadBookAdapter = new DownloadBookAdapter(this);
            downloadBookAdapter.setEnableLoadMore(true);
            mBooks.setLayoutManager(new GridLayoutManager(this, 1));
            // 设置下拉刷新和上拉加载更多的风格     参数1：应用程序上下文，参数2：是否具有上拉加载更多功能
            BGANormalRefreshViewHolder refreshViewHolder = new BGANormalRefreshViewHolder(this, false);
            // 设置下拉刷新
            refreshViewHolder.setRefreshViewBackgroundColorRes(R.color.color_F3F5F4);//背景色
            refreshViewHolder.setPullDownRefreshText(UIUtils.getString(R.string.refresh_pull_down_text));//下拉的提示文字
            refreshViewHolder.setReleaseRefreshText(UIUtils.getString(R.string.refresh_release_text));//松开的提示文字
            refreshViewHolder.setRefreshingText(UIUtils.getString(R.string.refresh_ing_text));//刷新中的提示文字
            // 设置下拉刷新和上拉加载更多的风格
            mRefreshLayout.setRefreshViewHolder(refreshViewHolder);
            mRefreshLayout.shouldHandleRecyclerViewLoadingMore(mBooks);
            mBooks.setAdapter(downloadBookAdapter);

            mp3Infos = db.selector(LocalMetadata.class).findAll();
            if (mp3Infos != null) {
                if (mp3Infos.size() > 0) {
                    downloadBookAdapter.replaceData(mp3Infos);
                }
            }

            downloadBookAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                @Override
                public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                    if (view.getId() == R.id.action_txt) {

                        LocalMetadata localMetadata = (LocalMetadata) adapter.getItem(position);

                        MusicActivity.mp3Infos = LocalUtils.getList(db,localMetadata.getBookid());

                        if (MusicActivity.mp3Infos != null) {
                            for (int i = 0; i < MusicActivity.mp3Infos.size(); i++) {
                                if (MusicActivity.mp3Infos.get(i).getId()==localMetadata.getId()){
                                    if (FileUtils.fileisDownload(MusicActivity.mp3Infos.get(i).getUrl()) && FileUtils.fileisDownload(MusicActivity.mp3Infos.get(i).getSrturl())) {
                                        Intent musicintent = new Intent(MyDownLoadActivity.this, MusicActivity.class);
                                        //当前播放的位置
                                        musicintent.putExtra("position", String.valueOf(position));
                                        //启动音乐播放界面
                                        startActivity(musicintent);
                                    }
                                }

                            }
                        }
                    }
                }
            });

        } catch (DbException e) {
            e.printStackTrace();
        }

    }
}
